// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {


    @property({
        displayName:"速度",
        tooltip:'移动的速度，值越大跑的越快'
    })
    sudu:number= 5;

   

    @property({
        displayName:"重力",
        tooltip:'自身重量，跳起的力量不变，值越大跳的越低，落下的越快，格斗游戏一般5以上'
    })
    gravityMax:number= 5;

    @property({
        displayName:"矩形框",
        tooltip:'自身的碰撞约束框'
    })
    Rect:cc.Rect=new cc.Rect(0,0,32,50);
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    public isJump(){
        let cy=50,tw=this.Rect.width/2;//
        
        this.node.x=Math.round(this.node.x);
        this.node.y=Math.round(this.node.y);
        let r=new cc.Rect(this.node.x-tw,this.node.y+2,this.Rect.width,4);
        let pw=32;
        let ph=32;
        let nodeX = r.x;
        let nodeY = r.y;
        let tx =(Math.floor(nodeX/pw));
        let ty =(Math.ceil(nodeY/ph));
        let iw =Math.floor(((r.width+ nodeX-1)/pw));
        let ih = Math.ceil((nodeY-r.height+1)/ph);
        for(let xt=tx;xt<=iw;xt++){
         for(let yt=ty;yt>=ih;yt--){
             let xn=(xt*pw).toString();
             let yn=(yt*ph).toString();
             if(window['hitObj']!=null&&window['hitObj'][xn]!=null&&window['hitObj'][xn][yn]!=null){
                for(let i=0;i<window['hitObj'][xn][yn].length;i++){
                    let r1=new cc.Rect(this.node.x-tw,this.node.y*-1,this.Rect.width,2);
                    let r2=window['hitObj'][xn][yn][i];
                    if(!((r1.x+r1.width)<=r2.x||(r2.x + r2.width)<=r1.x||(r1.y+r1.height)<=r2.y||(r2.y+r2.height)<=r1.y)){//相交
                       return true;
                    }
                }
             }
         }
        }
        return false;
    }
    public hitHeadFun:Function;
    public hitWallFun:Function;
    private hitRect(){
    
        let cy=this.Rect.height;
        let bo=false,tw=this.Rect.width/2;
        this.node.x=Math.round(this.node.x);
        this.node.y=Math.round(this.node.y);
        let r=new cc.Rect(this.node.x-tw,this.node.y+cy,this.Rect.width,cy);
        let pw=32;
        let ph=32;
        let nodeX = r.x;
        let nodeY = r.y;
        let tx =(Math.floor(nodeX/pw));
        let ty =(Math.ceil(nodeY/ph));
        let iw =Math.floor(((r.width+ nodeX-1)/pw));
        let ih = Math.ceil((nodeY-r.height+1)/ph);
        for(let xt=tx;xt<=iw;xt++){
         for(let yt=ty;yt>=ih;yt--){
             let xn=(xt*pw).toString();
             let yn=(yt*ph).toString();
             if(window['hitObj']!=null&&window['hitObj'][xn]!=null&&window['hitObj'][xn][yn]!=null){
                let length=window['hitObj'][xn][yn].length;
                for(let i=0;i<length;i++){
                    let r1=new cc.Rect(this.node.x-tw,this.node.y*-1-cy,this.Rect.width,cy);
                    let r2=window['hitObj'][xn][yn][i];
                    if(!((r1.x+r1.width)<=r2.x||(r2.x + r2.width)<=r1.x||(r1.y+r1.height)<=r2.y||(r2.y+r2.height)<=r1.y)){//没有相交
                        let num=0;
                        if(this.pverLocation!=null){
                            num=Math.abs(this.node.x-this.pverLocation.x)+Math.abs(this.node.y-this.pverLocation.y);
                        }
                        if(this.pverLocation==null||num==0){
                            this.bounce=0;
                            this.node.y=window['hitObj'][xn][yn][0].y*-1;
                        }else{
                            let nx=(this.node.x-this.pverLocation.x)/num;
                            let ny=(this.node.y-this.pverLocation.y)/num;
                            if(ny==1){
                                if(r1.y>r2.y){
                                    this.bounce=0;
                                    if(this.hitHeadFun!=null){
                                        this.hitHeadFun(r2['id']);
                                    }
                                }
                            }else if(ny==-1){
                                this.bounce=0;
                                bo=true;
                            }
                            if(nx){
                                if(this.hitWallFun!=null){
                                    this.hitWallFun(r2['id'],nx);
                                }
                            }
                            let nr=r1.intersection(new cc.Rect(),r2);
                            let k=0;
                            this.node.x-=nx*nr.width;
                            this.node.y-=ny*nr.height;
                        }
                       
                    }
                }
             }
         }
        }
        this.pverLocation=null;
      
      }
     
    start () {

    }
    public bounce:number=0;
    public pzbo=false;
    private ptArr:Array<any>=[];
    private pverLocation=null;
    public setXY(x=null,y=null){
        let bo:boolean=false;
        let p={"x":this.node.x,"y":this.node.y};
        if(y!==null)
        {
            //let ny=Math.abs(this.node.y-y);
            this.node.y=y;
            bo=true;
        }
        if(x!==null){
            this.node.x=x;
            bo=true;
        }
        if(bo){
            this.pverLocation=p;
            this.hitRect();
            if(this.updateMapLocation!=null&&this.node.x!=p.x){
                this.updateMapLocation();
            }
      
        }
    }
   public updateMapLocation:Function;
   private zhongli:number=0;
   private onWorld()
    {
        let i=this.gravityMax-this.bounce;
        //  if(this.zhongli>this.zhongliMax){this.zhongli=this.zhongliMax;}
        if(i>16){i=16;}
        this.setXY(null,this.node.y-i);
     
        this.bounce-=this.gravityMax/7;
      
    }
    update (dt) {
     //   this.pverLocation=null;
        this.onWorld();
     //   this.hitRect();
    }
}
